﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ZombieGame
{
    public class Pathfinder
    {
        public Pathfinder(Vector2 start, Vector2 target, bool[,] world)
        {
            this.start = start;
            this.target = target;
            this.world = world;
            path = new List<Node>();
        }

        public class Node
        {
            public Vector2 Position;
            public int Direction;
            public int Parent;
            public int ID;
            public int Steps;
            public Node(Vector2 p, int d)
            {
                Position = p;
                Direction = d;
            }

            public override bool Equals(object obj)
            {
                return ((Node)obj).Position == Position;
            }
        }

        Vector2 start;
        Vector2 target;
        List<Node> path;
        List<Node> final;
        bool[,] world;

        public void BeginFindPath()
        {
            path.Add(new Node(start, -1));
            if (!world[(int)start.X, (int)start.Y - 1])
                ProgressFindPath(new Node(new Vector2((int)start.X, (int)start.Y - 1), 0) { Parent = 0, ID = path.Count, Steps = 1 });
            if (!world[(int)start.X + 1, (int)start.Y])
                ProgressFindPath(new Node(new Vector2((int)start.X + 1, (int)start.Y), 1) { Parent = 0, ID = path.Count, Steps = 1 });
            if (!world[(int)start.X, (int)start.Y + 1])
                ProgressFindPath(new Node(new Vector2((int)start.X, (int)start.Y + 1), 2) { Parent = 0, ID = path.Count, Steps = 1 });
            if (!world[(int)start.X - 1, (int)start.Y])
                ProgressFindPath(new Node(new Vector2((int)start.X - 1, (int)start.Y), 3) { Parent = 0, ID = path.Count, Steps = 1 });
        }

        private void ProgressFindPath(Node node)
        {
            if (!path.Contains(node))
            {
                path.Add(node);
                if ((int)target.X == (int)node.Position.X && (int)target.Y == (int)node.Position.Y)
                {
                    CalculateFinalPath(node);
                    return;
                }
                if (node.Direction != 2 && !world[(int)node.Position.X, (int)node.Position.Y - 1])
                    ProgressFindPath(new Node(new Vector2((int)node.Position.X, (int)node.Position.Y - 1), 2) { Parent = node.ID, ID = path.Count });
                if (node.Direction != 3 && !world[(int)node.Position.X + 1, (int)node.Position.Y])
                    ProgressFindPath(new Node(new Vector2((int)node.Position.X + 1, (int)node.Position.Y), 3) { Parent = node.ID, ID = path.Count });
                if (node.Direction != 0 && !world[(int)node.Position.X, (int)node.Position.Y + 1])
                    ProgressFindPath(new Node(new Vector2((int)node.Position.X, (int)node.Position.Y + 1), 0) { Parent = node.ID, ID = path.Count });
                if (node.Direction != 1 && !world[(int)node.Position.X - 1, (int)node.Position.Y])
                    ProgressFindPath(new Node(new Vector2((int)node.Position.X - 1, (int)node.Position.Y), 1) { Parent = node.ID, ID = path.Count });
            }
        }

        private void CalculateFinalPath(Node n)
        {
            
        }
    }
}
